// 上传头像接口
export default defineEventHandler(async (event) => {
  const authHeader = getHeader(event, 'authorization')
  
  // 模拟延迟
  await new Promise(resolve => setTimeout(resolve, 800))

  // 验证 token
  if (!authHeader || !authHeader.startsWith('Bearer ')) {
    throw createError({
      statusCode: 401,
      message: '未登录或 token 无效'
    })
  }

  const token = authHeader.substring(7)

  if (!token.startsWith('token_')) {
    throw createError({
      statusCode: 401,
      message: 'token 格式错误'
    })
  }

  // 实际项目中这里会处理文件上传
  // 这里只是返回一个模拟的图片URL
  const randomSeed = Math.random().toString(36).substring(7)
  const avatarUrl = `https://api.dicebear.com/7.x/avataaars/svg?seed=${randomSeed}`

  return {
    code: 200,
    message: '上传成功',
    data: {
      url: avatarUrl
    }
  }
})


